package cn.edu.nuc.ssm.controller;



import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.nuc.ssm.entity.Admin;

import cn.edu.nuc.ssm.services.interfaces.AdminService;
import cn.edu.nuc.ssm.services.interfaces.GoodsService;
import cn.edu.nuc.ssm.util.PageInfo;


@Controller
public class AdminController {

	@Autowired
	private AdminService adminService;
	@Autowired
	private GoodsService goodsService;
	
	@RequestMapping(value="/adminlogin",method=RequestMethod.GET)
	public String login2(){
		
		return "admin-sign-in";
	}
	
	@RequestMapping(value="/adminlogin",method=RequestMethod.POST)
	public String login(Admin admin,HttpSession session){
			
			
				try {
					admin = adminService.login(admin.getAname(),admin.getApwd());
					session.setAttribute("admin", admin);
					return "redirect:/a/" + admin.getAid() + "/home";
				} catch (Exception e) {
					e.printStackTrace();
				}		
					
			return "redirect:adminlogin";
		}
	@RequestMapping(value="/a/{aid}/home",method=RequestMethod.GET)
	public String index(
			@PathVariable("aid") int aid,
			@RequestParam(name="current",defaultValue="1") int current,
			HttpSession session, 
			Model model){
		Admin admin = (Admin)session.getAttribute("admin");
		
		if( admin == null || admin.getAid() != aid ){
			return "redirect:/adminlogin";
		}
		
		
		PageInfo pi = goodsService.namelist("",current);
		model.addAttribute("pi", pi);
		return "admin-goods";
	}
	@RequestMapping(value="/a/{aid}/home",method=RequestMethod.POST)
	public String indexname(
			String gname,
			@RequestParam(name="current",defaultValue="1") int current,
			HttpSession session, 
			Model model){

		PageInfo pi = goodsService.namelist(gname,current);
		model.addAttribute("pi", pi);
		return "admin-goods";
	}

}
